package net.jalbum.filters;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;

import javax.swing.ImageIcon;

import net.jalbum.filterManager.FilterProperties.FilterCategory;
import se.datadosen.jalbum.Msg;

public class GrayScaleFilterPlugin extends BasicFilter {

	private static final long serialVersionUID = 1720559385062856569L;

	public GrayScaleFilterPlugin() {
		this.name = Msg.getString(this, "filters.grayScale.name");
		this.shortName = Msg.getString(this, "filters.grayScale.shortName");
		this.icon = new ImageIcon(this.getClass().getResource("res/grayscale.png"));
		this.author = "David Fichtmueller";
		this.version = "1.0";
		this.description = Msg.getString(this, "filters.grayScale.description");
		this.category = FilterCategory.OTHER;
		this.prescale = false;
		this.postscale = true;
	}

	@Override
	public BufferedImage renderImage(final BufferedImage bi) {
		// the following code is by David Ekholm from the GrayscaleFilter
		final Graphics2D g = bi.createGraphics();
		final RenderingHints rhs = g.getRenderingHints();

		final ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
		final ColorConvertOp theOp = new ColorConvertOp(cs, rhs);

		final BufferedImage dstImg = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_RGB);
		theOp.filter(bi, dstImg);

		return dstImg;
	}
}
